package com.dindang.checkline.mvp.ui.activity.adapter;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dindang.checkline.R;
import com.dindang.checkline.app.utils.TextTransformUtils;
import com.dindang.checkline.mvp.greendao.entity.CheckPiont;
import com.dindang.checkline.mvp.model.entity.CheckLineEntity;
import com.dindang.checkline.mvp.model.entity.TaskDetail;

import java.util.Date;
import java.util.List;

/**巡检路线适配器*/
public class ChecklineAdapter extends RecyclerView.Adapter<ChecklineAdapter.ViewHolder>{

    List<CheckPiont> list;
    String[] states = {"未巡检", "正常", "异常"};
    String status;
    public ChecklineAdapter(List<CheckPiont> list) {
        this.list = list;
    }
    MyOnClickListener myOnClickListener;
    boolean isOrder,isStart;

    public void setMyOnClickListener(MyOnClickListener myOnClickListener) {
        this.myOnClickListener = myOnClickListener;
    }

    public void setIsOrder(boolean isOrder,boolean isStart){
        this.isOrder = isOrder;
        this.isStart = isStart;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.checkline_item, parent, false);
        ChecklineAdapter.ViewHolder viewHolder = new ChecklineAdapter.ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //是否有序
        if(isOrder){
            holder.maintenance_iv_progress.setVisibility(View.GONE);
            holder.maintenance_iv_progress_order.setVisibility(View.VISIBLE);
            holder.maintenance_iv_progress_order.setText((position+1)+"");
        }else {
            holder.maintenance_iv_progress.setVisibility(View.VISIBLE);
            holder.maintenance_iv_progress_order.setVisibility(View.GONE);
        }
        //路线名称
        holder.tv_line_name.setText(list.get(position).getcName());
        //状态
        holder.tv_address.setText(list.get(position).getcPosition());
//        0未开始巡检  1 巡检中无异常  2 巡检中有异常  3 巡检结束无异常 4.巡检结束有异常
          status= list.get(position).getcStatus();
        String textSource = "未完成巡检请<font color='#0557c0'><u>点击这里</u></font>完成巡检";
        String lastTime = "";
        String time = list.get(position).getRItemLastTime();
        if(time.contains(".0")){
            time = time.replace(".0","");
        }
        if(time.contains(",")){
            lastTime = new Date(time)+"";
        }else {
            lastTime = time;
        }
        switch (status) {
            case "0":// 未开始巡检
                if(isStart){
                    holder.tv_scan.setVisibility(View.VISIBLE);
                    holder.scan_img.setVisibility(View.VISIBLE);
                    holder.scan_img_haschecked.setVisibility(View.GONE);
                    holder.tv_scan.setText(Html.fromHtml(textSource));
                }else {
                    holder.tv_scan.setVisibility(View.INVISIBLE);
                    holder.scan_img.setVisibility(View.INVISIBLE);
                    holder.scan_img_haschecked.setVisibility(View.GONE);
                }
                break;
            case "1":// 巡检中无异常
                holder.tv_scan.setVisibility(View.VISIBLE);
                holder.scan_img.setVisibility(View.GONE);
                holder.scan_img_haschecked.setVisibility(View.VISIBLE);
                holder.scan_img_haschecked.setBackgroundResource(R.mipmap.zhengchang);
                holder.scan_img_haschecked.setText("正常");
                holder.tv_scan.setText(Html.fromHtml(textSource));
                break;
            case "2":// 异常
                holder.tv_scan.setVisibility(View.VISIBLE);
                holder.scan_img.setVisibility(View.GONE);
                holder.scan_img_haschecked.setVisibility(View.VISIBLE);
                holder.scan_img_haschecked.setBackgroundResource(R.mipmap.yichang);
                holder.scan_img_haschecked.setText("异常");
                holder.tv_scan.setText(Html.fromHtml(textSource));
                break;
            case "3":// 3 巡检结束无异常
                holder.tv_scan.setVisibility(View.VISIBLE);
                holder.tv_scan.setText("已完成巡检："+lastTime);
                holder.scan_img.setVisibility(View.GONE);
                holder.scan_img_haschecked.setVisibility(View.VISIBLE);
                holder.scan_img_haschecked.setBackgroundResource(R.mipmap.zhengchang);
                holder.scan_img_haschecked.setText("正常");
                break;
            case "4":// 4.巡检结束有异常
                holder.tv_scan.setVisibility(View.VISIBLE);
                holder.tv_scan.setText("已完成巡检："+lastTime);
                holder.scan_img.setVisibility(View.GONE);
                holder.scan_img_haschecked.setVisibility(View.VISIBLE);
                holder.scan_img_haschecked.setBackgroundResource(R.mipmap.yichang);
                holder.scan_img_haschecked.setText("异常");
                break;
            default:
                holder.tv_scan.setVisibility(View.INVISIBLE);
                holder.scan_img.setVisibility(View.INVISIBLE);
                holder.scan_img_haschecked.setVisibility(View.GONE);
                break;
        }
       holder.scan_img.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               myOnClickListener.onScanClick(position);
           }
       });
        holder.tv_scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myOnClickListener.onScanClick(position);
            }
        });
        holder.scan_img_haschecked.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myOnClickListener.onScanClick(position);
            }
        });
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

//    @Override
//    public void onClick(View v) {
////        if(list.get(position).c_status.equals("0")){//未巡检,点击开始巡检
//            myOnClickListener.onScanClick(position);
////        }
//    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView scan_img;
        TextView tv_line_name;
        TextView tv_address;
        TextView tv_scan;
        TextView scan_img_haschecked;
        ImageView maintenance_iv_progress;
        TextView maintenance_iv_progress_order;
        ViewHolder(View itemView) {
            super(itemView);
            scan_img = itemView.findViewById(R.id.scan_img);
            tv_line_name = itemView.findViewById(R.id.check_line_name);
            tv_address = itemView.findViewById(R.id.check_line_address);
            tv_scan = itemView.findViewById(R.id.check_click_here);
            scan_img_haschecked = itemView.findViewById(R.id.scan_img_haschecked);
            maintenance_iv_progress = itemView.findViewById(R.id.maintenance_iv_progress);
            maintenance_iv_progress_order = itemView.findViewById(R.id.maintenance_iv_progress_order);
        }
    }
    public interface MyOnClickListener {
        void onScanClick(int position);
    }
}